﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Net;
using System.Xml;
using System.Text;
using System.Xml.Linq;
namespace LightSwitchApplication
{
    public partial class SearchPhysiciansAndMedicalStaffs
    {
        private Stream physStream;
        private Stream assistStream;

        partial void gridDeleteSelected_CanExecute(ref bool result)
        {
            // Write your code here.
        }

        partial void gridDeleteSelected_Execute()
        {
            // Write your code here.
            this.Physicians.DeleteSelected();
        }

        private void client1_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            physStream = e.Result;
        }

        private void client2_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            assistStream = e.Result;
        }

        partial void retrievePhysicianData_Execute()
        {
            try
            {
                //Load the Physicians
                WebClient client1 = new WebClient();
                Uri physiciansList = new Uri("http://agas.heroku.com/physicians.xml?key=bf4e28785ab0560951dd0766f8059c4a");
                client1.OpenReadAsync(physiciansList);
                client1.OpenReadCompleted += new OpenReadCompletedEventHandler(client1_OpenReadCompleted);

                while (physStream == null)
                    ;

                XDocument physDoc = XDocument.Load(physStream);

                var physicians = from p in physDoc.Descendants("physician")
                                 select new
                                 {
                                     firstName = p.Descendants("firstname").First().Value,
                                     lastName = p.Descendants("lastname").First().Value,
                                     phone = p.Descendants("phone").First().Value
                                 };

                foreach (var physician in physicians)
                {
                    Physician existing = this.DataWorkspace.ApplicationData.Physicians.Where(p => p.FirstName.Equals(physician.firstName) && p.LastName.Equals(physician.lastName) && p.Phone == physician.phone).Execute().FirstOrDefault();
                    if (existing == null)
                    {
                        Physician p = this.DataWorkspace.ApplicationData.Physicians.AddNew();
                        p.FirstName = physician.firstName;
                        p.LastName = physician.lastName;
                        p.Phone = physician.phone;
                    }
                }

                //Load the Physician Assitants
                WebClient client2 = new WebClient();
                Uri assistantList = new Uri("http://agas.heroku.com/medical_staffs.xml?key=bf4e28785ab0560951dd0766f8059c4a");
                client1.OpenReadAsync(assistantList);
                client1.OpenReadCompleted += new OpenReadCompletedEventHandler(client2_OpenReadCompleted);

                while (assistStream == null)
                    ;

                XDocument assistDoc = XDocument.Load(assistStream);

                var assistants = from a in assistDoc.Descendants("medical_staff")
                                 select new
                                 {
                                     firstName = a.Descendants("firstname").First().Value,
                                     lastName = a.Descendants("lastname").First().Value,
                                     phone = a.Descendants("phone").First().Value
                                 };

                foreach (var assistant in assistants)
                {
                    Physician existing = this.DataWorkspace.ApplicationData.Physicians.Where(a => a.FirstName.Equals(assistant.firstName) && a.LastName.Equals(assistant.lastName) && a.Phone == assistant.phone).Execute().FirstOrDefault();
                    if (existing == null)
                    {
                        Physician a = this.DataWorkspace.ApplicationData.Physicians.AddNew();
                        a.FirstName = assistant.firstName;
                        a.LastName = assistant.lastName;
                        a.Phone = assistant.phone;
                        a.IsAssistant = true;
                    }
                }

                this.DataWorkspace.ApplicationData.SaveChanges();
                physStream = null;
                assistStream = null;
            }
            catch
            {
                    
            }
        }
    }
}

namespace UserCode
{
    public partial class SearchPhysiciansAndMedicalStaffs
    {
    }
}
